#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "rtc_misc.h"
#include "product_info.h"

LV_IMG_DECLARE(icon_info);

ACTIVITY_OPS_IMPLEMENT_DECLARE(system, &icon_info, ACTIVITY_ATTR_FLAG_APP);

// ##
struct system_user_t
{
    void *device;
    void *model;
    void *mac;
    void *sn;
    void *sw_ver;
    void *built_time;
    void *author;
};

// ##
static void app_start(void *content)
{

}

// ##
static void app_stop(void *content)
{

}

// ##
static void app_prepare(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    activity->custom = ams_malloc(sizeof(struct system_user_t));
}

// ##
static void *app_create(void *content)
{
    static lv_style_t style;
    lv_style_init(&style);

    lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_BLACK);

    lv_style_set_pad_top(&style, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_bottom(&style, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_left(&style, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_right(&style, LV_STATE_DEFAULT, 10);

    lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_style_set_text_letter_space(&style, LV_STATE_DEFAULT, 0);
    lv_style_set_text_line_space(&style, LV_STATE_DEFAULT, 20);
    lv_style_set_text_decor(&style, LV_STATE_DEFAULT, LV_TEXT_DECOR_NONE);

    lv_style_set_text_font(&style, LV_STATE_DEFAULT, &lv_font_montserrat_16);
    lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

    // ##
    ams_activity_t *activity = (ams_activity_t *)content;
    struct system_user_t *user = (struct system_user_t *)activity->custom;
    if(!activity->ui)
    {
        activity->ui = lv_obj_create(NULL, NULL);
    }
    lv_obj_add_style(activity->ui, LV_LABEL_PART_MAIN, &style);

    // ##
    #define X_START     40
    #define Y_START     20
    #define Y_DELTA     50
    int16_t cnt = 0;
    #define Y_POS(N)   (Y_START + (N) * Y_DELTA)
    user->device = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->device, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->device, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->device, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->device, "Device : %s", DEVICE_NAME);

    // ##
    user->model = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->model, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->model, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->model, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->model, "Model : %s", DEVICE_MODEL);

    // ##
    user->mac = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->mac, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->mac, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->mac, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->mac, "MAC : %02X-%02X-%02X-%02X-%02X-%02X",
                                    0x12, 0x34, 0x56, 0x78, 0x90, 0xAB);

    // ##
    user->sn = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->sn, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->sn, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->sn, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->sn, "SN : %d", 12345678);

    // ##
    user->sw_ver = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->sw_ver, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->sw_ver, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->sw_ver, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->sw_ver, "Version : %s", SOFTWARE_VERSION);

    // ##
    user->built_time = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->built_time, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->built_time, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->built_time, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->built_time, "Built at : %s, %s", __TIME__, __DATE__);

    // ##
    user->author = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->author, LV_LABEL_PART_MAIN, &style);
    lv_label_set_align(user->author, LV_LABEL_ALIGN_LEFT);
    lv_obj_set_pos(user->author, X_START, Y_POS(cnt++));
    lv_label_set_text_fmt(user->author, "Author : %s", MANUFACTURER_NAME);

    return activity->ui;
}

// ##
static void *app_destroy(void *content)
{
    // ams_activity_t *activity = (ams_activity_t *)content;
    // lv_obj_clean(activity->ui);

    return NULL;
}

// ##
static void app_event_handle(void *event)
{

}

// ##
static void app_gesture_handle(void *event)
{
    ams_gesture_t evt = *((ams_gesture_t *)event);
    switch(evt)
    {
        case GESTURE_SLIDE_TO_TOP:
            break;
        case GESTURE_SLIDE_TO_BOTTOM:
            break;
        case GESTURE_SLIDE_TO_LEFT:
            break;
        case GESTURE_SLIDE_TO_RIGHT:
            ams_switch_by_name("applist", DIR_SLIDE_TO_RIGHT);
            break;
    }
}
